package TabbedLesson
{
	import Assets.Controls.MyWindow;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mvc.AbstractView;
	import mvc.Controller;
	
	import mx.containers.ApplicationControlBar;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.HSlider;
	import mx.controls.Label;
	import mx.controls.List;
	import mx.events.CloseEvent;
	import mx.events.ListEvent;
	import mx.events.SliderEvent;
	
	import util.Observable;

	public class WindowedLessonView extends AbstractView
	{
		private var lessonModel_object:Object = new Object;
		private var lessonController_object:Object = new Object;
		private var lessonName_string:String;
		//private var lessonController_object:Object = new Object;
		private var nativeWindowContainer_sprite:Sprite = new Sprite;
		private var openedLessonWindow:MyWindow;
		//private var children_array:Array=new Array;
		
		//private var testButton:Button = new Button;
		
		[Embed(source="Assets/icons/LessonBack.jpg")]
		[Bindable]
		private var lessonBackground:Class;
		
		
		private var editLesson_button:Button;
		private var editMode_bool:Boolean;
		private var component_button:Button;
		private var component_list:List;
		private var addComponent_button:Button;
		private var compName_array:Array;
		
		//--------------------------control Panel Variables--------------------------------------------------------------//
		private var appControlBar:ApplicationControlBar;
		private var prev_button:Button;
		private var next_button:Button;
		private var stop_button:Button;
		private var playPause_button:Button;
		private var mainTimer_seekBar:HSlider;
		private var curTime_label:Label;
		
		private var playPauseButtonView_string:String;
		private var currentTime_int:int;
		
		[Embed(source="Assets/icons/prev.png")]
		[Bindable]
		private var prev_buttonIcon:Class;
		
		[Embed(source="Assets/icons/next.png")]
		[Bindable]
		private var next_buttonIcon:Class;
		
		[Embed(source="Assets/icons/stop.png")]
		[Bindable]
		private var stop_buttonIcon:Class;
		
		[Embed(source="Assets/icons/play.png")]
		[Bindable]
		private var play_buttonIcon:Class;
		
		[Embed(source="Assets/icons/pause.png")]
		[Bindable]
		private var pause_buttonIcon:Class;
			
		//-----------------------------------------------------------------------------------------------------------------//
		public function WindowedLessonView(model:Observable, controller:Controller, container:Sprite, lessonName:String)
		{
			super(model, controller);
			this.lessonModel_object =this.getModel();
			this.lessonController_object=this.getController();
			this.nativeWindowContainer_sprite = container;
			this.lessonName_string = lessonName;
			this.openedLessonWindow = new MyWindow(this,this.nativeWindowContainer_sprite,this.lessonName_string);
			this.openedLessonWindow.setDimensions(this.nativeWindowContainer_sprite.width,this.nativeWindowContainer_sprite.height);
			this.openedLessonWindow.openWindow();
			//this.openedLessonWindow.maximize();
			//this.children_array.push(this.testButton);
			this.initWindowLesson();
			this.addMouseUpEventToWindow();
		}
		
		private function initWindowLesson():void
		{
			var infoObj_object:Object = new Object;
			infoObj_object.lessonName = this.lessonName_string;
			infoObj_object.lessonBackground = this.lessonBackground;
			this.openedLessonWindow.setWindowAppearance(infoObj_object);
			this.initControlPanel();
			this.openedLessonWindow.newWindow.addEventListener(Event.CLOSING,this.lessonClosing); 
		/*	this.testButton.x =this.openedLessonWindow.getWidth()/2;
			this.testButton.y=this.openedLessonWindow.getHeight()/2;
			this.addChildToStage(this.testButton);
			*/
			this.editLesson_button = new Button;
			this.editLesson_button.label = "Edit";
			this.editLesson_button.x=10;
			this.editLesson_button.y=50;
			this.editLesson_button.addEventListener(MouseEvent.CLICK, this.initEditMode);
			this.addChildToStage(this.editLesson_button);
			
			this.component_button = new Button;
			this.component_button.label = "components";
			this.component_button.x=10;
			this.component_button.y=80;
			this.component_button.visible=false;
			this.component_button.addEventListener(MouseEvent.CLICK,this.initComponentList);
			this.addChildToStage(this.component_button);
			
			
			this.compName_array = new Array;
			this.compName_array = (["Glossary","Slides","Audio","Chat","Video","Topics","ScratchPad","Assessment","PasteNote"]);
			//this.compName_array.push("Video", "Slides");			
			this.component_list = new List;
			this.component_list.dataProvider = this.compName_array;
			this.component_list.validateNow();
			this.component_list.editable = false;
			this.component_list.visible = false;
			this.component_list.selectable = true;
			this.component_list.x=10;
			this.component_list.y=110;
			this.component_list.width=100;
			this.component_list.height = 300;
			this.component_list.addEventListener(ListEvent.ITEM_CLICK,this.listItemSelected);
			this.addChildToStage(this.component_list);
			
			this.addComponent_button = new Button;
			this.addComponent_button.label = "AddComponent";
			this.addComponent_button.x=10;
			this.addComponent_button.y=this.component_list.y+this.component_list.height+10;
			this.addComponent_button.visible=false;
			this.addComponent_button.addEventListener(MouseEvent.CLICK,this.addComponent);
			this.addChildToStage(this.addComponent_button);
		
			//this.openedLessonWindow.addChildren(this.children_array);
		}
		
		private function initControlPanel():void
		{
			this.appControlBar=new ApplicationControlBar;
			this.appControlBar.x=0;
			this.appControlBar.y=0;
			this.appControlBar.width = this.openedLessonWindow.getWidth();
			this.appControlBar.setStyle("backgroundColor",0xffffff);
			this.appControlBar.setStyle("alpha",1.0);
			this.addChildToStage(this.appControlBar);
			
			this.prev_button = new Button;
			this.prev_button.width = 40;
			this.prev_button.height=34;
			this.prev_button.setStyle("backgroundColor",0xffffff);
			this.prev_button.setStyle("alpha",1.0);
			this.prev_button.setStyle("icon",this.prev_buttonIcon);
			this.prev_button.addEventListener(MouseEvent.CLICK,this.prevButtonPressed);
			this.appControlBar.addChild(this.prev_button);
			
			this.next_button = new Button;
			this.next_button.width = 40;
			this.next_button.height=34;
			this.next_button.setStyle("backgroundColor",0xffffff);
			this.next_button.setStyle("alpha",1.0);
			this.next_button.setStyle("icon",this.next_buttonIcon);
			this.next_button.addEventListener(MouseEvent.CLICK,this.nextButtonPressed);
			this.appControlBar.addChild(this.next_button);
			
			this.stop_button = new Button;
			this.stop_button.width = 40;
			this.stop_button.height=34;
			this.stop_button.setStyle("backgroundColor",0xffffff);
			this.stop_button.setStyle("alpha",1.0);
			this.stop_button.setStyle("icon",this.stop_buttonIcon);
			this.stop_button.addEventListener(MouseEvent.CLICK, this.stopButtonPressed);
			this.appControlBar.addChild(this.stop_button);
			
			this.playPause_button = new Button;
			this.playPause_button.width = 40;
			this.playPause_button.height=34;
			this.playPause_button.setStyle("backgroundColor",0xffffff);
			this.playPause_button.setStyle("alpha",1.0);
			this.playPause_button.setStyle("icon",this.play_buttonIcon);
			this.playPause_button.addEventListener(MouseEvent.CLICK,this.playPauseButtonPressed);
			this.appControlBar.addChild(this.playPause_button);
			
			this.mainTimer_seekBar = new HSlider;
			this.mainTimer_seekBar.minimum = 0;
			this.mainTimer_seekBar.maximum = 3600;
			this.mainTimer_seekBar.tickInterval = 60;
			this.mainTimer_seekBar.snapInterval = 1;
			this.mainTimer_seekBar.allowTrackClick = true;
			this.mainTimer_seekBar.liveDragging = true;
			this.mainTimer_seekBar.width =this.appControlBar.width-400 ;
			this.mainTimer_seekBar.addEventListener(SliderEvent.CHANGE,this.updateTime);
			this.appControlBar.addChild(this.mainTimer_seekBar);
			
			this.curTime_label = new Label;
			this.curTime_label.text="0.0";
			this.curTime_label.setStyle("fontWeight","bold");
			this.appControlBar.addChild(this.curTime_label);
			
			this.playPauseButtonView_string = "PauseView";
			this.currentTime_int=0;
		}
		
		private function initEditMode(event:MouseEvent):void
		{
			this.editLesson_button.label="Save";
			this.editLesson_button.removeEventListener(MouseEvent.CLICK,this.initEditMode);
			this.editLesson_button.addEventListener(MouseEvent.CLICK,this.saveLesson);
			this.component_button.visible=true;
			
			this.editMode_bool = true;
			
			this.informEditModeToModel();
		}
		
		private function saveLesson(event:MouseEvent):void
		{
			this.editLesson_button.label="Edit";
			this.editLesson_button.removeEventListener(MouseEvent.CLICK, this.saveLesson);
			this.editLesson_button.addEventListener(MouseEvent.CLICK, this.initEditMode);
			this.component_button.visible=false;
			this.component_list.visible=false;
			
			this.editMode_bool=false;
			this.informEditModeToModel();
			
			var infoObj_object:Object = new Object;
			infoObj_object.message = "saveLesson";
			this.sendInfoToController(infoObj_object);
		}
				
		private function informEditModeToModel():void
		{
			var infoObj_object:Object = new Object;
			infoObj_object.message = "setEditModeStatus";
			infoObj_object.editMode = this.editMode_bool;
			this.sendInfoToController(infoObj_object);
			
		}
		
		private function initComponentList(event:MouseEvent):void
		{
			this.component_list.visible = true;
			this.component_button.removeEventListener(MouseEvent.CLICK,this.initComponentList);
			this.component_button.addEventListener(MouseEvent.CLICK,this.removeComponentList);
		}
		
		private function removeComponentList(event:MouseEvent):void
		{
			this.component_list.visible=false;
			this.component_button.removeEventListener(MouseEvent.CLICK,this.removeComponentList);
			this.component_button.addEventListener(MouseEvent.CLICK,this.initComponentList);
		}
		
		private function listItemSelected(event:Event):void
		{
			trace(this.component_list.selectedItem.toString());
			this.addComponent_button.visible=true;
			
		}
		
		private function addComponent(event:MouseEvent):void
		{
			this.addComponent_button.visible=false;
			var infoObj_object:Object = new Object;
			infoObj_object.message = "openNewComponent";
			infoObj_object.name = this.component_list.selectedItem.toString();
			this.sendInfoToController(infoObj_object);
		}
		
		public function addChildToStage(displayObj:DisplayObject):void  //function to be used by lesson components to add themselves on stage
		{
			this.openedLessonWindow.addChildren(displayObj);
		}
		
		public function removeChildFromStage(displayObj:DisplayObject):void
		{
			this.openedLessonWindow.removeChild(displayObj);
		}
		//-----------------------------------------Close Handler Functions-------------------------------------------------------------//
		private function lessonClosing(event:Event):void
		{
			event.preventDefault();
			Alert.show("Do you relly want to exit the lesson?","Confirmation",Alert.YES|Alert.NO,this.openedLessonWindow.newWindow,this.lessonCloseAlertHandler,null,Alert.YES);
			
		}
		
		private function lessonCloseAlertHandler(event:CloseEvent):void
		{
			if(event.detail==Alert.YES)
			{
				this.checkEditMode();
			}
		}
		
		private function checkEditMode():void
		{
			if(this.editMode_bool)
			{
				Alert.show("Do you relly want to save the changes?","Confirmation",Alert.YES|Alert.NO,this.openedLessonWindow.newWindow,this.saveLessonAlertHandler,null,Alert.YES);
			}
			else
			this.closeLesson();
		}
		
		private function saveLessonAlertHandler(event:CloseEvent):void
		{
			if(event.detail==Alert.YES)
			{
				var infoObj_object:Object = new Object;
				infoObj_object.message = "saveLesson";
				this.sendInfoToController(infoObj_object);
				this.closeLesson();
			}
			else
				this.closeLesson();
		}
		
		private function closeLesson():void
		{
			this.openedLessonWindow.newWindow.removeEventListener(Event.CLOSING, this.lessonClosing);
			this.openedLessonWindow.closeWindow();
		}
		
		//------------------------------------------------------------------------------------------------------------------------------//
		
		//--------------------------------------Function for resizing the child components----------------------------------------------//
		private function addMouseUpEventToWindow():void
		{
			this.openedLessonWindow.windowCanvas.addEventListener(MouseEvent.MOUSE_UP,this.mouseUpEventHandler);
		}
		
		private function mouseUpEventHandler(event:MouseEvent):void
		{
			var infoObj_object:Object=new Object;
			infoObj_object.message = "mouseUpEvent";
			infoObj_object.event = event;
			
			trace("WindowedLessonView------->event:mouseup :"+event+" infoObj.event :"+infoObj_object.event);
			this.sendInfoToController(infoObj_object);
		}
		
		//-------------------------------------------------------------------------------------------------------------------------------//
		
		//--------------------------------------------------ControlPanel Related Operations------------------------------------------------------//
	private function playPauseButtonPressed(event:MouseEvent):void
	{
		var infoObj_object:Object = new Object;
		trace("PlayPause button pressed");
		if(this.playPauseButtonView_string=="PauseView")
		{
			infoObj_object.message="lessonPlayedFromControlPanel";
			this.setPlayView();
		}
		else
		{
			infoObj_object.message="lessonPausedFromControlPanel";
			this.setPauseView();
		}
		
		this.sendInfoToLessonModel(infoObj_object);
				
	}
	
	private function setPlayView():void
	{
		this.playPause_button.setStyle("icon",this.pause_buttonIcon);
		this.playPauseButtonView_string="PlayView";
		trace("showing pause icon");
	}
	
	private function setPauseView():void
	{
		this.playPause_button.setStyle("icon",this.play_buttonIcon);
		this.playPauseButtonView_string="PauseView";
		trace("showing play icon");
	}
	private function stopButtonPressed(event:MouseEvent):void
	{
		this.resetTimer();
		var infoObj_object:Object=new Object;
		infoObj_object.message="lessonStoppedFromControlPanel";
		this.sendInfoToLessonModel(infoObj_object);
	}
	
	private function nextButtonPressed(event:MouseEvent):void
	{
		
	}
	
	private function prevButtonPressed(event:MouseEvent):void
	{
		
	}
	
	private function resetTimer():void
	{
		this.currentTime_int=0;
		this.updateTimeLine();
		this.updateTimeLabel();
		this.setPauseView();
		
	}
	
	private function updateTime(event:SliderEvent):void
	{
		this.currentTime_int=this.mainTimer_seekBar.value;
		this.updateTimeLabel();
		
		var infoObj_object:Object = new Object;
		infoObj_object.message = "syncCurrentTimeWithSeekBar";
		infoObj_object.time=this.currentTime_int;
		this.sendInfoToLessonModel(infoObj_object);
	}
	
	private function updateTimeLabel():void
	{
		this.curTime_label.text=this.currentTime_int.toString();
		this.curTime_label.validateNow();
	}
	
	private function updateTimeLine():void
	{
		this.mainTimer_seekBar.value = this.currentTime_int;
		this.mainTimer_seekBar.validateNow();
	}
	
	
		//--------------------------------------------------------------------------------------------------------------------------------//
		
		//-------------------------------------------------------------Information Exchange Functions------------------------------------//
		private function sendInfoToController(infoObj_object:Object):void
		{
			this.lessonController_object.incomingMessageGateway(infoObj_object);
		}
		
		override public function update(model:Observable, infoObj_object:Object):void
		{
			trace("WindowedLessonView --> Updating Windowed Lesson View");
			super.update(model,infoObj_object);
			var message_string:String = infoObj_object.message.toString();
			trace("WindowedLessonView --> Message from Model : "+message_string);
			
			switch(message_string)
			{
				case "closeWindow":
					this.closeLesson();
					break;
			}
		}
		
		public function incomingMessageGateway(infoObj_object:Object):void
		{
			var message_string:String = infoObj_object.message.toString();
			trace("WindowedLessonView-----> message: "+message_string);
			
			switch(message_string)
			{
				case "windowClosed":
					this.sendInfoToController(infoObj_object);
					break;
					
				case "lessonPlayed":
						this.setPlayView();
						break;
						
				case "lessonPaused":
						this.setPauseView();
						break;
			
				case "updateCurrentTime":
					this.currentTime_int = infoObj_object.time;
					this.updateTimeLine();
					this.updateTimeLabel();
					break;
			}
		}
		
		private function sendInfoToLessonModel(infoObj_object:Object):void
		{
			this.lessonModel_object.incomingMessageGateway(infoObj_object);
		}
		
	}
}